package com.common.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.springframework.scheduling.annotation.Async;


/**
 * 系统常量类
 * @author lijinfeng
 * @date 2014-07-31
 * @version 2.0
 * @description 用于定义系统常量
 */
public final class ServiceConstant {
	
	/**
	 * 应用系统域
	 */
	public static final String DOMAIN = "";
	/**
	 * 有效状态
	 */
	public static final String SYSTEM_ON = "1";
	/**
	 * 有效状态
	 */
	public static final String SYSTEM_TWO = "2";
	/**
	 * 有效状态
	 */
	public static final String SYSTEM_THREE = "3";
	/**
	 * 有效状态
	 */
	public static final String SYSTEM_FOUR = "4";
	/**
	 * 禁用用户
	 */
	public static final String OFF_DEVELOPER = "0";
	/**
	 * 开启用户
	 */
	public static final String ON_DEVELOPER="1";
	/**
	 * 有效状态
	 */
	public static final String SYSTEM_FIVE = "5";
	/**
	 * 有效状态
	 */
	public static final String SYSTEM_NO = "X";
	/**
	 * 无效状态
	 */
	public static final String SYSTEM_OFF = "0";
	/**
	 * 行政权力最后两位编码
	 */
	public static final String FOLDER_CODE = "00";
	
	/**
	 * 错误响应码300
	 */
	public static final String SYSTEM_ERROR = "300";
	/**
	 * 成功响应码200
	 */
	public static final String SYSTEM_SUCCESS = "200";	
	/**
	 * 用户会话信息 
	 */
	public static final java.lang.String USER_TOCKEN = "_Invalidate_Session_Token_";
	/**
	 * 开发者类型
	 */
	public static final String USER_TYPE = "KFZ";

	/**
	 * 事项操作动作编码
	 * 1 新增
	 * 2 编辑
	 * 3 删除
	 * 4 查询
	 * @author lijinfeng
	 * @version 2014-03-22
	 */
	public static enum ItemAction { 
		Add("ADD"),Edit("EDIT"),Delete("DELETE"),Query("QUERY");
		//操作动作
		private String action;
		//操作动作
		ItemAction(String action){
			this.action = action;
		}
		public String getAction() { 
			return this.action; 
		} 
	}
	/**
	 * 返回登录方式
	 * @param account
	 * @return
	 */
	public static String getLoginType(String account){
		if(CTools.isEmail(account)){
			return "EMAIL";
		}else if(CTools.isMobile(account)){
			return "MOBILE";
		}
		return "ACCOUNT";
	}
	
	/**
	 * 应用状态定义
	 * 0 审核中
	 * 1 已上线
	 * 2 已下线
	 * 3 未通过
	 * @author lijinfeng
	 * @version 2014-03-19
	 */
	public static enum ServiceAppState { 
		CHECKING(0),ONLINE(1),OFFLINE(2),REJECT(3);
		private String state;
		ServiceAppState(int i){
			this.state = String.valueOf(i);
		}
		/**
		 * 返回应用状态
		 * @return
		 */
		public String getState() { 
			return this.state; 
		} 
		/**
		 * 应用状态映射表
		 * @return
		 */
		public static List<Map<String,Object>> getAppStateMap(){
			List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
			Map<String,Object> map0= new HashMap<String,Object>();
			map0.put("key", "");
			map0.put("value", "全部");
			list.add(map0);
			Map<String,Object> map1= new HashMap<String,Object>();
			map1.put("key", "0");
			map1.put("value", "审核中");
			list.add(map1);
			Map<String,Object> map2= new HashMap<String,Object>();
			map2.put("key", "1");
			map2.put("value", "已上线");
			list.add(map2);
			Map<String,Object> map3= new HashMap<String,Object>();
			map3.put("key", "2");
			map3.put("value", "已下线");
			list.add(map3);
			Map<String,Object> map4= new HashMap<String,Object>();
			map4.put("key", "3");
			map4.put("value", "未通过");
			list.add(map4);
			return list;
		}
	}
	
	/**
	 * API状态定义
	 * 0 审核中
	 * 1 已上线
	 * 2 未通过
	 * @author lijinfeng
	 * @version 2014-03-19
	 */
	public static enum ServiceApiState { 
		CHECKING(0),ONLINE(1),REJECT(2);
		private String state;
		ServiceApiState(int i){
			this.state = String.valueOf(i);
		}
		public String getState() { 
			return this.state; 
		} 
	}

	/**
	 * 平台动态类型定义[ac_developer_notice]
	 * 0 开发者通过实名认证
	 * 1 开发者发布新的主题(或官方)
	 * 2 开发者注册APP审核通过并发布上线
	 * 3 管理员审核通过了API发布
	 * 4 开发者应用与API关联
	 * @author lijinfeng
	 * @version 2014-03-19
	 */
	public static enum ServiceNotifyType { 
		DEVELOPER(0),TITLE(1),APP(2),API(3),APP$API(4);
		private int type;
		ServiceNotifyType(int i){
			this.type = i;
		}
		public int getType() { 
			return this.type; 
		} 
	}


	/**
	 * 互动类型定义编码
	 * 0 反馈
	 * 1 投诉
	 * 2 咨询
	 * @author lijinfeng
	 * @version 2014-03-19
	 */
	public static enum ServiceSnsType { 
		FEEDBACK(0),COMPLAIN(1),CONSULT(2);
		private String state;
		ServiceSnsType(int i){
			this.state = String.valueOf(i);
		}
		public String getType() { 
			return this.state; 
		} 
	}
	/**
	 * 论坛发帖状态
	 * 0删除
	 * 1审核通过
	 * 2待审核
	 * @author ZHEN
	 *
	 */
	public static enum ServiceForumType { 
		DELETE(0),SHTG(1),DSH(2);
		private String state;
		ServiceForumType(int i){
			this.state = String.valueOf(i);
		}
		public String getType() { 
			return this.state; 
		} 
	}
	/**
	 * 开发者状态
	 * @author ZHEN
	 *3:注册未激活 
	 *2:邮件激活账号 (此时可以登录)
	 *1:账号已进行实名认证(审核通过)
	 *0:开发者提交实名认证信息
	 */
	public static enum DeveloperStatus{
		Zc(3),Jh(2),Smrz(1),Tjrz(0);
		private String status;
		DeveloperStatus(int i){
			this.status = String.valueOf(i);
		}
		public String getStatus() { 
			return this.status; 
		} 
	}
	
	public enum HeartBeatFrequency {  
		FIVE("FIVE","5分钟", "0 0/5 * * * ?"),//每5分钟执行一次
	    TEN("TEN","10分钟" ,"0 0/10 * * * ?"),//每10分钟一次
	    TWENTY("TWENTY","20分钟" ,"0 0/20 * * * ?"),//每20分钟一次
	    THIRTY("THIRTY", "30分钟","0 0/30 * * * ?"),//每30分钟一次
	    FOURTY("FOURTY","40分钟" ,"0 0/40 * * * ?"),//每40分钟一次
	    FIFTY("FIFTY","50分钟" ,"0 0/50 * * * ?"),//每50分钟一次
	    SIXTY("SIXTY", "每小时","0 0 0/1 * * ?"),//每小时一次
		TWOHOURS("TWOHOURS", "每2小时","0 0 0/2 * * ?"),//每2小时一次
		THREEHOURS("THREEHOURS","每3小时" ,"0 0 0/3 * * ?"),//每3小时一次
		FOURHOURS("FOURHOURS", "每4小时","0 0 0/4 * * ?"),//每4小时一次
		FIVEHOURS("FIVEHOURS", "每5小时","0 0 0/5 * * ?"),//每5小时一次
		SIXHOURS("SIXHOURS","每6小时" ,"0 0 0/6 * * ?"),//每6小时一次
		SEVENHOURS("SEVENHOURS", "每7小时","0 0 0/7 * * ?"),//每7小时一次
		EIGHTHOURS("EIGHTHOURS","每8小时" ,"0 0 0/8 * * ?"),//每8小时一次
		NINEHOURS("NINEHOURS","每9小时" ,"0 0 0/9 * * ?"),//每9小时一次
		TENHOURS("TENHOURS", "每10小时","0 0 0/10 * * ?"),//每10小时一次
		ELEVENHOURS("ELEVENHOURS", "每11小时","0 0 0/11 * * ?"),//每11小时一次
		TWELVEHOURS("TWELVEHOURS","每12小时" ,"0 0 0/12 * * ?"),//每12小时一次
		DAY("DAY","每天" ,"0 0 0 * * ?"),//每天0点执行一次
		MONTY("TWELVEHOURS","每月" ,"0 0 0 1 * ?"),//每月一号0点执行一次
		YEAR("TWELVEHOURS","每年" ,"0 0 0 1 0 ?");//每年一月一号0点执行一次
		private String times;
		private String name;
	    private String cronExpression;

	    private HeartBeatFrequency(String times,String name, String cronExpression) {
	        this.times = times;
	        this.cronExpression = cronExpression;
	        this.name = name;
	    }  
	    // 普通方法  
	    public static String cronExpression(String seconds) {  
	        for (HeartBeatFrequency c : HeartBeatFrequency.values()) {  
	            if (c.getTimes().equals(seconds)) {  
	                return c.getCronExpression();  
	            }  
	        }  
	        return null;  
	    }
	    
	    public static String getNameStr(String seconds) {  
	        for (HeartBeatFrequency c : HeartBeatFrequency.values()) {  
	            if (c.getTimes().equals(seconds)) {  
	                return c.getName();  
	            }  
	        }  
	        return null;  
	    }
	    
		public String getTimes() {
			return times;
		}
		public void setTimes(String times) {
			this.times = times;
		}
		public String getCronExpression() {
			return cronExpression;
		}
		public void setCronExpression(String cronExpression) {
			this.cronExpression = cronExpression;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
	    
	} 
	
	/**
	 * 发送短信验证码
	 * @param url
	 * @param phone
	 * @param text
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static int sendMobileCheckCode(String url,String phone,String text) throws UnsupportedEncodingException{
		HttpClient client = new HttpClient();
		HttpMethod httpGet=new GetMethod(url+"/igp/sms/sendSms?phone="+phone+"&text="+URLEncoder.encode(text,"UTF-8"));
		try {
			client.executeMethod(httpGet);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
        return httpGet.getStatusCode();  
	}
	/**
	 * 发送邮件
	 * @param senderName
	 * @param receiverEmail
	 * @param title
	 * @param message
	 */
	@SuppressWarnings("deprecation")
	@Async
	public static int sendMailCheckCode(String url,String subject,String senderName,String receiverEmail,String message){
		if(senderName==null){
			senderName = URLEncoder.encode("浪潮政务数据平台");
		}
//		String text = "<!DOCTYPE html>"
//									+"<html xmlns='http://www.w3.org/1999/xhtml'>"
//									+"<head>"
//									+"<meta charset='UTF-8'/>"
//									+"<title>"+subject+"</title>"
//									+"<style type=text/css>"
//									+".textdark {color: #444444;font-family:'Microsoft YaHei', Open Sans;font-size: 13px;line-height: 150%;text-align: left;}"
//									+"</style>"
//									+"</head>"
//									+"<body>"
//											+"<table border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color:#28b779; height:52px;'>"
//											+"<tbody><tr>"
//											+"	<td align='center'>"
//											+"		<center>"
//											+"			<table border='0' cellpadding='0' cellspacing='0' width='100%' style='height:100%;'>"
//											+"				<tbody><tr>"	
//											+"					<td align='left' valign='middle' style='padding-left:20px;'>"
//											+"<span style='font-size:14px;'>"+senderName+"&nbsp; - &nbsp;<b style='color:white;'>"+subject+"</b>：</span>"
//											+"					</td>"
//											+"				</tr></tbody>"
//											+"			</table>"
//											+"		</center>"
//											+"</td>"
//									+"		</tr></tbody></table>"
//									+"<table border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color:#f8f8f8;border-bottom:1px solid #e7e7e7;'>"
//									+"<tbody><tr><td><center><table border='0' cellpadding='0' cellspacing='0' width='100%' style='height:100%;'>"
//									+"<tbody><tr><td valign='top' style='padding:20px;text-align:left;'><h3><b style='color:black;'>"+URLEncoder.encode("您好！")+"</b></h3>"
//									+"<br><div class='textdark'>"
//									+URLEncoder.encode(message)
//									+"</div><br></td></tr></tbody></table></center></td></tr></tbody></table>"
//									+"<table border='0' cellpadding='0' cellspacing='0' width='100%' style='background-color:#28b779;'>"
//									+"		<tbody><tr><td align='center'><center><table border='0' cellpadding='0' cellspacing='0' width='100%' style='height:100%;'><tbody><tr>"
//									+" <td align='right' valign='middle' class='textwhite' style='font-size:12px;padding:20px;'>"
//									+URLEncoder.encode("版权所有© 2014 ")+senderName+"."
//									+"</td></tr></tbody></table>"
//									+"</center></td></tr></tbody></table>"
//									+"</body>";
		
		HttpClient client = new HttpClient();
		HttpMethod httpGet=new GetMethod(url+"/igp/mail/sendMail?to="+receiverEmail+"&subject="+URLEncoder.encode(subject)+"&text="+URLEncoder.encode(message));
		try {
			client.executeMethod(httpGet);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
        return httpGet.getStatusCode();  
	}
	 public static String decodeSpecialCharsWhenLikeUseBackslash(String content)
	    {
	        // 单引号是oracle字符串的边界,oralce中用2个单引号代表1个单引号
	        String afterDecode = content.replaceAll("'", "''");
	        // 由于使用了/作为ESCAPE的转义特殊字符,所以需要对该字符进行转义
	        // 这里的作用是将"a/a"转成"a//a"
	        afterDecode = afterDecode.replaceAll("/", "//");
	        // 使用转义字符 /,对oracle特殊字符% 进行转义,只作为普通查询字符，不是模糊匹配
	        afterDecode = afterDecode.replaceAll("%", "/%");
	        // 使用转义字符 /,对oracle特殊字符_ 进行转义,只作为普通查询字符，不是模糊匹配
	        afterDecode = afterDecode.replaceAll("_", "/_");
	        return afterDecode;
	    }
	public static void main(String[] args){
		System.out.println(CTools.getUUID());
	}

	/**
	 * 平台会话标识
	 */
	public static final String SHARE_PLATFORM="IGDP";
	
	/**
	 * 用户类型
	 */
	public static final java.lang.String TYPE_CODE = "KFZ";
	/**
	 * 事项操作动作编码
	 * 收费配置员	ROLE_BANK
	 * 超级管理员	ROLE_SUPER
	 * 平台管理员	ROLE_ADMIN
	 * 系统管理员	ROLE_APP
	 * 区划管理员	ROLE_AREA
	 * 部门管理员	ROLE_ORGAN
	 * 收费管理员	ROLE_CHARGE
	 * 技术测试员	ROLE_TEST
	 * 部门配置员	ROLE_CONFIG
	 * 部门法制处	ROLE_LEGAL
	 * 法制办管理员	ROLE_LAW
	 * 处室管理员	ROLE_OFFICE
	 * 收费员	ROLE_FEE
	 * 业务管理员	ROLE_BUSINSS
	 * 业务员	ROLE_OPERATOR
	 * 执法员	ROLE_ENFORCER
	 * 普通用户	ROLE_USER
	 * @author lijinfeng
	 * @version 2014-03-22
	 */
	public static enum RoleCode{ 
		Super("ROLE_SUPER"),Platform("ROLE_APP"),Admin("ROLE_ADMIN"),
		Area("ROLE_AREA"),Law("ROLE_LAW"),Bank("ROLE_BANK"),Test("ROLE_TEST"),
		Organ("ROLE_ORGAN"),Legal("ROLE_LEGAL"),Config("ROLE_CONFIG"),
		Office("ROLE_OFFICE"),Charge("ROLE_CHARGE"),Operator("ROLE_OPERATOR"),
		Business("ROLE_BUSINSS"),Fee("ROLE_FEE"),User("ROLE_USER");
		//角色编码
		private String code;
		//角色编码赋值
		RoleCode(String code){
			this.code = code;
		}
		//返回角色编码
		public String getCode() { 
			return this.code; 
		} 
	}
	/**
	 * 事项服务状态编码
	 * 0 变更
	 * 1 草案
	 * 2 试用
	 * 3 已提交
	 * 4 已废置
	 * 5 已发布
	 * 6 备案
	 * @author lijinfeng
	 * @version 2014-03-19
	 */
	public static enum ServiceState { 
		Zero(0),One(1),Two(2),Three(3),Four(4),Five(5),Six(6);
		private String state;
		ServiceState(int i){
			this.state = String.valueOf(i);
		}
		public String getState() { 
			return this.state; 
		} 
	}
}
